package org.ihaibi.cmp.util;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import net.sf.json.JSONObject;


/**
 * 
 * 类功能说明：httpclient工具类,基于httpclient 4.x
 * Title: HttpClientUtils.java
 * 
 * @author zhenghui
 * @date 2014-3-7 下午7:48:58
 * @version V1.0
 */
public class HttpClientUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);
    
    public static final String IP_PORT = PropertiesUtils.readProperty("restful.url");
    
    public static final String CONTEXT = "api";
    
    public static final String VERSION = "v1";
    
    public static final String ROOT_URL = IP_PORT+"/"+CONTEXT+"/"+VERSION;
    
    public static final String GET_COUNT = "count";
    
    /**
     * get请求String
     * 
     * @param url
     * @return
     */
    public static String doGet(String url) {
        try {
            return RestClient.getClient().getForObject(url, String.class);
        } catch (Exception e) {
        }

        return "";
    }
    
    
    
    
    
   
    
  
    
    
    /**
     * get请求返回Java对象
     * 
     * @param url
     * @return
     */
    public static Map<String,Object>  doGetForMap(String url) {
        try {
           String result = RestClient.getClient().getForObject(url, String.class);
              return   JsonUtils.json2map(result);
        } catch (Exception e) {
            LOGGER.error("GET请求出错：{}", url, e);
        }
        return null;
    }
    

  
  
    /**
     * get请求返回Java对象
     * 
     * @param url
     * @return
     */
    public static <T> List<T>  doGetForList(String url,Class<T> returnClassName) {
        try {
           String result = RestClient.getClient().getForObject(url, String.class);
              return   JsonUtils.json2list(result, returnClassName);
        } catch (Exception e) {
            LOGGER.error("GET请求出错：{}", url, e);
        }
        return null;
    }
    

    /**
     * post请求
     * 
     * @param url
     * @return
     */
    public static String doPost(String url, Object params) {
        RestTemplate restTemplate = RestClient.getClient();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        JSONObject jsonObj = JSONObject.fromObject(params);

        jsonObj = JsonUtils.filterNull(jsonObj);

        HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);

        String result = restTemplate.postForObject(url, formEntity, String.class);
        return result;
    }

    /**
     * @doPut
     * 
     * @param url
     * 
     * @param params
     */
    public static void doPut(String url, String id,Object params) {
        RestTemplate restTemplate = RestClient.getClient();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        JSONObject jsonObj = JSONObject.fromObject(params);
        jsonObj = JsonUtils.filterNull(jsonObj);

        HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
        restTemplate.put(url +id, formEntity);
        
    }
    

}